<?php
/**
 * Created by PhpStorm.
 * User: meng
 * Date: 2023/12/21
 * Time: 10:35
 */

namespace app\admin\controller;

use app\admin\service\SettingsService;
use app\admin\validator\SettingItemValidator;
use app\admin\validator\SettingValidator;
use lib\annotations\AdminPermission;
use lib\common\CommonController;

class Settings extends CommonController
{
    #[AdminPermission]
    public function index (SettingsService $service): void {
        $data = $service->settingList();
        $this->success($data);
    }

    #[AdminPermission]
    public function create (SettingsService $service): void {
        $params = $this->request->all();
        (new SettingValidator())->goCheck($params, 'create');
        $service->createSetting($params);
        $this->success();
    }

    #[AdminPermission]
    public function update ($id, SettingsService $service): void {
        $params = $this->request->all();
        (new SettingValidator())->goCheck($params, 'update');
        $service->updateSetting($id, $params);
        $this->success();
    }

    #[AdminPermission]
    public function delete ($id, SettingsService $service): void {
        $service->deleteSetting($id);
        $this->success();
    }

    #[AdminPermission]
    public function settingItemList ($id, SettingsService $service): void {
        $data = $service->settingItemList($id);
        $this->success($data);
    }

    #[AdminPermission]
    public function createSettingItem ($id, SettingsService $service): void {
        $params = $this->request->all();
        (new SettingItemValidator())->goCheck($params);
        $service->createSettingItem($id, $params);
        $this->success();
    }

    #[AdminPermission]
    public function updateSettingItem ($id, $itemId, SettingsService $service): void {
        $params = $this->request->all();
        (new SettingItemValidator())->goCheck($params);
        $service->updateSettingItem($id, $itemId, $params);
        $this->success();
    }

    #[AdminPermission]
    public function deleteSettingItem ($id, $itemId, SettingsService $service): void {
        $service->deleteSettingItem($id, $itemId);
        $this->success();
    }


    #[AdminPermission]
    public function saveSettingValue (SettingsService $service): void {
        $service->saveSettingValue($this->request->post());
        $this->success();
    }
}
